package com.example.login.service;

import com.example.login.entity.ClassInfo;
import com.example.login.entity.Course;
import com.example.login.entity.User;
import com.example.login.entity.UserCourse;
import com.example.login.mapper.CourseMapper;
import com.example.login.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {

    @Autowired
    private CourseMapper courseMapper;

    public boolean chooseCourse(String userName, String course_name, String semester) {
        User user = courseMapper.findByUsername(userName);
        Course course = courseMapper.findByCourseName(course_name);
        if (user == null || course == null) {
            return false;
        }
        UserCourse userCourse = new UserCourse();
        userCourse.setUserName(userName);
        userCourse.setCourse_name(course_name);
        userCourse.setSemester(semester);
        courseMapper.saveInfo(userCourse);
        return true;
    }

    public String getClassNameByUserName(String userName) {
        User user = courseMapper.findByUserName(userName);
        if (user != null) {
            return user.getClass_name();
        }
        return null;
    }


    public ClassInfo getClassInfoByClassName(String className) {
        ClassInfo classInfo = courseMapper.findByClassName(className);
        if (classInfo != null) {
            return classInfo;
        }
        return null;
    }

    public boolean checkIfExistInUserCourse(String userName, String course_name, String semester) {
        boolean isExist = false;
        List<UserCourse> userCourse = courseMapper.findByUserNameAndCourseNameAndSemester(userName, course_name, semester);
        if (userCourse != null) {
            isExist = true;
        }
        return isExist;
    }

}
